package com.ruoyi.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.WebsocketConstants;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.tws.api.domain.Driver;
import com.ruoyi.tws.domain.TwsCompany;
import com.ruoyi.tws.service.*;
import lombok.RequiredArgsConstructor;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.web.page.Condition;
import com.ruoyi.tws.domain.TwsQuestion;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 咨询投诉Controller
 * 
 * @author ruoyi
 * @date 2022-01-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/question")
public class TwsQuestionController extends BaseController
{
    private final ITwsQuestionService iTwsQuestionService;
    private final ITwsNoticeService twsNoticeService;
    private final IDriverService iDriverService;
    private final ITwsCompanyService twsCompanyService;

    /**
     * 查询咨询投诉列表
     */
    @PreAuthorize(hasPermi="tws:question:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query,TwsQuestion twsQuestion)
    {
        LambdaQueryWrapper<TwsQuestion> lqw = new LambdaQueryWrapper<TwsQuestion>();

//        if (StringUtils.isNotBlank(twsQuestion.getContent())){
//            lqw.eq(TwsQuestion::getContent ,twsQuestion.getContent());
//        }
        if (twsQuestion.getCompanyId() != null){
            lqw.eq(TwsQuestion::getCompanyId,twsQuestion.getCompanyId());
        }
        if (twsQuestion.getDelFlag() != null){
            lqw.eq(TwsQuestion::getDelFlag ,twsQuestion.getDelFlag());
        }
        if (twsQuestion.getType() != null){
            lqw.eq(TwsQuestion::getType ,twsQuestion.getType());
        }
        if (twsQuestion.getStatus() != null){
            lqw.eq(TwsQuestion::getStatus ,twsQuestion.getStatus());
        }
        if (StringUtils.isNotBlank(twsQuestion.getUserName())){
            lqw.like(TwsQuestion::getUserName ,twsQuestion.getUserName());
        }
        if (twsQuestion.getCreateBy() != null){
            lqw.eq(TwsQuestion::getCreateBy ,twsQuestion.getCreateBy());
        }
//        if (twsQuestion.getStar() != null){
//            lqw.eq(TwsQuestion::getStar ,twsQuestion.getStar());
//        }
        lqw.orderByDesc(TwsQuestion::getCreateTime);
        IPage<TwsQuestion> list = iTwsQuestionService.page(Condition.getPage(query),lqw);
        return getDataTable(list);
    }

    /**
     * 导出咨询投诉列表
     */
    @PreAuthorize(hasPermi = "tws:question:export")
    @Log(title = "咨询投诉", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TwsQuestion twsQuestion) throws IOException
    {
        LambdaQueryWrapper<TwsQuestion> lqw = new LambdaQueryWrapper<TwsQuestion>(twsQuestion);
        List<TwsQuestion> list = iTwsQuestionService.list(lqw);
        ExcelUtil<TwsQuestion> util = new ExcelUtil<TwsQuestion>(TwsQuestion. class);
        util.exportExcel(response, list, "question");
    }

    /**
     * 获取咨询投诉详细信息
     */
    @PreAuthorize(hasPermi="tws:question:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iTwsQuestionService.getById(id));
    }

    /**
     * 新增咨询投诉
     */
    @PreAuthorize(hasPermi="tws:question:add" )
    @Log(title = "咨询投诉" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TwsQuestion twsQuestion) {
//        String username = SecurityUtils.getUsername();
//        SysUser user = userService.getUserInfo(username, SecurityConstants.INNER).getData().getSysUser();
//        twsQuestion.setUserName(user.getNickName());
        TwsCompany company = twsCompanyService.getById(SecurityUtils.getTenantId());
        twsQuestion.setCompanyId(company.getId());
        twsQuestion.setUserName(company.getCompanyName());
        if (twsQuestion.getType() == 1){
            twsNoticeService.newTwsNotice("admin", WebsocketConstants.TWS_NOTICE_TYPE.CERT_CONSULT.getKey(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_CONSULT.getName());
        }else if (twsQuestion.getType() == 2){
            twsNoticeService.newTwsNotice("admin",WebsocketConstants.TWS_NOTICE_TYPE.CERT_COMPLAIN.getKey(),WebsocketConstants.TWS_NOTICE_TYPE.CERT_COMPLAIN.getName());
        }
        return toAjax(iTwsQuestionService.save(twsQuestion) ? 1 : 0);
    }

    /**
     * 修改咨询投诉
     */
    @PreAuthorize(hasPermi="tws:question:edit")
    @Log(title = "咨询投诉" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TwsQuestion twsQuestion) {
        return toAjax(iTwsQuestionService.updateById(twsQuestion) ? 1 : 0);
    }

    /**
     * 删除咨询投诉
     */
    @PreAuthorize(hasPermi="tws:question:remove" )
    @Log(title = "咨询投诉" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iTwsQuestionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 回复
     * @param twsQuestion
     * @return
     */
    @PostMapping("/answer")
    @Transactional
    public AjaxResult answer(@RequestBody TwsQuestion twsQuestion){
        twsQuestion.setIsFinish(1);
        if (twsQuestion.getStatus() == 1){
            String userName = twsCompanyService.getUserNameByCompanyId(twsQuestion.getCompanyId());
            if (twsQuestion.getType() == 1){
                if(StringUtils.isNotBlank(userName)) {
                    twsNoticeService.newTwsNotice(userName, WebsocketConstants.TWS_NOTICE_TYPE.CERT_CONSULT_FINISH.getKey(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_CONSULT_FINISH.getName());
                }
            }else {
                if(StringUtils.isNotBlank(userName)) {
                    twsNoticeService.newTwsNotice(userName, WebsocketConstants.TWS_NOTICE_TYPE.CERT_COMPLAIN_FINISH.getKey(),WebsocketConstants.TWS_NOTICE_TYPE.CERT_COMPLAIN_FINISH.getName());
                }
            }
        }else if (twsQuestion.getStatus() == 2){
            Driver driver = iDriverService.getById(twsQuestion.getDriverId());
            if (twsQuestion.getType() == 1){
                twsNoticeService.newTwsNotice(driver.getTelephone(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_CONSULT_FINISH.getKey(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_CONSULT_FINISH.getName());
            }else {
                twsNoticeService.newTwsNotice(driver.getTelephone(),WebsocketConstants.TWS_NOTICE_TYPE.CERT_COMPLAIN_FINISH.getKey(),WebsocketConstants.TWS_NOTICE_TYPE.CERT_COMPLAIN_FINISH.getName());
            }
        }
        return toAjax(iTwsQuestionService.updateById(twsQuestion) ? 1 : 0);
    }
}
